package com.turku.folipass;

import java.io.IOException;
import java.util.concurrent.ExecutionException;

import net.sf.json.JSON;
import net.sf.json.xml.XMLSerializer;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.os.AsyncTask;


public class FoliAdaptor {

public JSON getXMLdataFromurl(String url){	
	try {
		return new xmlData().execute(url).get();
	} catch (InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (ExecutionException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return null;
}
	
	private class xmlData extends AsyncTask<String, Void, JSON> {
		JSON objJson;
		@Override
		protected  JSON doInBackground(String... url) {
			// TODO Auto-generated method stub
			try {

				DefaultHttpClient httpClient = new DefaultHttpClient();
				HttpPost httpPost = new HttpPost(url[0]);
				HttpResponse httpResponse;
				httpResponse = httpClient.execute(httpPost);
				HttpEntity httpEntity = httpResponse.getEntity();
				objJson = new XMLSerializer().read(EntityUtils.toString(httpEntity));
				
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return objJson;
		};
	}	
	
}
